/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.control;

import java.util.ArrayList; 
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.collections.map.MultiValueMap;
import org.eclipse.swt.graphics.Image;
import org.mags.common.EngineContext;
import org.mags.remas.control.commons.ControlUtils;
import org.mags.remas.logic.EntityLogic;
import org.mags.remas.model.entity.EntityCategory;
import org.mags.remas.model.entity.EntityComment;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.view.commons.ImageProvider;
import org.mags.remas.view.widgets.TreeActionTransmitter;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class PagedCommentsController extends EntityRelationshipController {	
	private EntityRelationship filter;
	private int requiredPage=0;
	private int pageSize=100;
	private long totalSize = 0;
	
	private int getLastPage() {
		int lastPage = (int)(totalSize/pageSize)-1;
		if((totalSize%pageSize)>0)
			lastPage = lastPage+1;
		return lastPage;
	}
	public void moveNextPage() {
		if(requiredPage+1<=getLastPage())
			requiredPage++;
	}
	public void movePrevPage() {
		if(requiredPage-1>=0)
			requiredPage--;
	}
	public void moveLastPage() {
		requiredPage = getLastPage();
	}
	public void moveFirstPage() {
		requiredPage = 0;
	}
	public long getTotalSize() {
		return totalSize;
	}
	public int getRequiredPage() {
		return requiredPage;
	}
	public void setRequiredPage(int requiredPage) {
		this.requiredPage = requiredPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public EntityRelationship getFilter() {
		return filter;
	}
	public void setFilter(EntityRelationship filter) {
		this.filter = filter;
		requiredPage = 0;
	}
	public PagedCommentsController() {
	}
	public boolean hasChildren(Object object) {
		return false;
	}
	public Object[] getChildren(Object object) {
		return null;
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Object[] getElements(Object object) {
		totalSize = EngineContext.getDataContext().getEntityProviderFactory().getEntityRelationshipProvider().countWithCommentsBySample(filter);
		MultiValueMap comments = EngineContext.getDataContext().getEntityProviderFactory().getEntityRelationshipProvider().findWithCommentsBySample(filter, requiredPage*pageSize, pageSize);
	    List<EntityRelationshipComment> list = new ArrayList<EntityRelationshipComment>();
	    for(Object _er: comments.keySet()) {
	    	EntityRelationship er = (EntityRelationship)_er;
			Collection collection = comments.getCollection(_er);
	    	for(Object _m: collection) {
	    		EntityComment comment = (EntityComment)_m;
	    		EntityRelationshipComment erc = new EntityRelationshipComment(er);
	    		erc.setEntityComment(comment);
	    		list.add(erc);
	    	}
	    }
	    Collections.sort(list, new Comparator() {
			@Override
			public int compare(Object o1, Object o2) {
				EntityRelationshipComment er1 = (EntityRelationshipComment)o1;
				EntityRelationshipComment er2 = (EntityRelationshipComment)o2;
				long diff = er2.getEntityComment().getEntityCommentId()-er1.getEntityComment().getEntityCommentId();
				if(diff>0)
					return 1;
				else if(diff<0)
					return -1;
				else 
					return 0;
			}
	     });
		return list.toArray();
	}
	public EntityRelationship refresh(EntityRelationship entityRelationship) {
		EntityRelationship er = EngineContext.getDataContext().getEntityProviderFactory().getEntityRelationshipProvider().getById(entityRelationship.getEntityRelationshipId());
		TreeActionTransmitter.getInstance().updateData(er);
		return er;
	}
	public void delete(List<EntityRelationship> relationshipsToDelete) {
		for(EntityRelationship entityRelationshipToDelete: relationshipsToDelete) {
			EntityRelationship parentEntityRelationship = entityRelationshipToDelete.getParent();
			EntityCategory entityCategory = entityRelationshipToDelete.getEntity().getEntityType().getEntityCategory();
			if(parentEntityRelationship==null) {
				parentEntityRelationship = new EntityRelationship();
				parentEntityRelationship.setEntity(entityRelationshipToDelete.getEntityParent());
				String hierarchicalPath = ControlUtils.getParentPath(entityRelationshipToDelete.getHierarchicalPath(), entityRelationshipToDelete.getEntity().getEntityId());
				parentEntityRelationship.setHierarchicalPath(hierarchicalPath);
			}
			EntityLogic.getInstance().delete(entityRelationshipToDelete, parentEntityRelationship, entityCategory);
		}		
	}	
	@Override
	public Image getImage(Object object) {
		return ImageProvider.getCommonImage("comments");
	}
	@Override
	public String getText(Object object) {
		EntityRelationshipComment entityRelationship = (EntityRelationshipComment)object;
		EntityComment comment = entityRelationship.getEntityComment();
		return "["+entityRelationship.getEntity().getCode() + ") " + entityRelationship.getEntity().getName()+"]     "+comment.getComment();
		
	}
	
}
